package cn.itsource.minio.controller;

import cn.itsource.minio.service.IMinioService;
import cn.itsource.util.AjaxResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.management.ObjectName;

@RestController
@RequestMapping("/minio")
public class MinioController {
    @Autowired
    private IMinioService minioService;
  @PostMapping("/upload")
  public AjaxResult upload(MultipartFile file){
    System.out.println(2222);
    try {
      return AjaxResult.me().setResultObj(minioService.upload(file));
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.me().setSuccess(false).setMessage("上传失败,请稍后再试!");
    }
  }

  /**
   *  通过附件名删除云服务器的附件
   * @param objectName
   * @return
   */
  @DeleteMapping ("/{objectName}")
  public AjaxResult delete(@PathVariable("objectName") String objectName){

    try {
      minioService.delete(objectName);
      return AjaxResult.me();
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.me().setSuccess(false).setMessage("上传失败,请稍后再试!");
    }
  }
}
